<template>
  <div class="input-container">
    <div class="input-title">
      <img src="@/assets/logo_back.png" class="title_back" @click="handleBack" />
      <span class="title_text">身份认证</span>
    </div>
    <div class="input-text">
      <p class="text-tips" style="margin: 32px 0 10px">手机号</p>
      <input
        class="input-name"
        type="text"
        v-model="form.phone"
        placeholder="请输入手机号"
        @blur="checkRule"
        :class="{ 'input-name-error': !phoneInputStatus }"
      />
      <div class="error-text" v-if="!phoneInputStatus">请输入11位手机号</div>

      <p class="text-tips" style="margin: 20px 0 10px">短信验证</p>
      <div style="position: relative">
        <input
          class="input-name"
          type="text"
          v-model="form.messageCode"
          placeholder="请输入短信验证码"
          @blur="checkRule"
          :class="{ 'input-name-error': !smsCodeInputStatus }"
        />
        <div class="sendCode" @click="sendCode">
          <span v-if="countdown <= 0">发送验证码</span>
          <span v-else>{{ countdown }}秒后可重新获取</span>
        </div>
      </div>
    </div>

    <div class="submit-operate">
      <button class="submit-btn" @click="handleNext" :disabled="!form.phone || !form.messageCode">下一步</button>
    </div>
    <div class="conclusion">
      <img src="@/assets/point.png" />
      <span>如果您忘记了绑定的手机号码，请联系管理员</span>
    </div>
    <!-- 图片验证码弹窗 -->
    <imgCodeModal ref="imgCodeModalRef" @success="handleImgCodeSuccess" />
  </div>
</template>

<script setup>
  import { ref, onMounted } from 'vue'
  import { checkSMSCode } from '@/api/user'
  import imgCodeModal from './img-code-modal.vue'
  import { Message } from '@arco-design/web-vue'
  import Cookies from 'js-cookie'

  const emit = defineEmits(['changeIndex'])
  const form = ref({
    phone: '',
    messageCode: '',
  })

  const phoneInputStatus = ref(true)
  const smsCodeInputStatus = ref(true)

  /**
   * 返回上一步
   */
  const handleBack = () => {
    emit('changeIndex', 1)
  }

  /**
   * 校验手机号
   */
  const checkRule = () => {
    if (form.value.phone.length != 11) {
      phoneInputStatus.value = false
    } else {
      phoneInputStatus.value = true
    }
  }

  /**
   * 发送短信验证码
   */
  const countdown = ref(0)
  const imgCodeModalRef = ref(null)
  const sendCode = () => {
    checkRule()
    // 触发图片验证校验
    if (phoneInputStatus.value == false) {
      Message.warning('请输入正确手机号')
      return
    }
    if (countdown.value > 0) return
    imgCodeModalRef.value.open({ phone: form.value.phone }, 'resetPassword')
  }

  /**
   * 短信发送成功
   */
  const handleImgCodeSuccess = () => {
    countdown.value = 59 // 立即进入倒计时
    const timer = setInterval(() => {
      countdown.value--
      if (countdown.value <= 0) {
        clearInterval(timer)
      }
    }, 1000)
  }

  const handleNext = () => {
    if (phoneInputStatus.value == false) {
      Message.warning('请输入正确手机号')
      return
    }
    if (!form.value.messageCode) {
      Message.warning('请输入短信验证码')
      return
    }
    checkSMSCode({
      phone: form.value.phone,
      messageCode: form.value.messageCode,
    }).then((res) => {
      console.log(res)
      if (res.code == 200) {
        if (res.result.status) {
          emit('changeIndex', 3, {
            phone: form.value.phone,
            messageCode: form.value.messageCode,
          })
        } else {
          Message.error(res.result.message)
        }
      }
    })
  }
</script>

<style lang="less" scoped>
  .input-title {
    display: flex;
    align-items: center;
    margin-left: -12px;

    .title_back {
      margin-right: 12px;
      width: 48px;
      height: 48px;
      cursor: pointer;
      &:hover {
        opacity: 0.8;
      }
    }
    .title_text {
      font-size: 32px;
      font-weight: 600;
      color: #1e1e1e;
    }
  }
  .text-tips {
    height: 22px;
    font-size: 14px;
    font-weight: 400;
    color: #86909c;
    line-height: 22px;
  }
  .login-container {
    position: relative;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    display: flex;
    justify-content: flex-start;
    .input-container {
      width: 398px;
      margin: 0 auto;
      .img-verify {
        display: flex;
        align-items: center;
        justify-content: space-between;

        .img-code {
          width: 100px;
          height: 50px;
          border-radius: 4px;
          border: 1px solid #c5c6cd;
          cursor: pointer;
        }
      }
      .input-text {
        .input-name {
          width: 398px;
          height: 50px;
          border-radius: 3px;
          font-size: 14px;
          font-weight: 400;
          opacity: 1;
          border: 1px solid #c5c6cd;
          outline-style: none;
          padding-top: 14px;
          padding-bottom: 14px;
          padding-left: 18px;
        }
        .input-name:focus {
          outline: none;
          border: 1px solid #165dff;
        }
        .input-name-error {
          outline: none;
          border: 1px solid #ff5b5a;
        }
      }
      .submit-operate {
        margin-top: 18px;
        .submit-btn {
          width: 398px;
          height: 50px;
          font-size: 16px;
          color: #ffffff;
          border-radius: 2px 2px 2px 2px;
          background: #165dff;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          box-sizing: border-box;
          font-weight: 400;
          line-height: 1.5715;
          white-space: nowrap;
          outline: none;
          cursor: pointer;
          transition: all 0.1s cubic-bezier(0, 0, 1, 1);
          &:disabled {
            background: rgba(22, 93, 255, 0.3);
          }
        }
      }
      .conclusion {
        margin-top: 24px;
        display: flex;
        justify-content: center;
        align-items: center;
        img {
          width: 15px;
          margin-right: 3px;
        }
        font-size: 14px;
        color: #1e1e1e;
      }
    }
  }
  .error-text {
    height: 22px;
    font-size: 14px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #ff5b5a;
    line-height: 22px;
    margin-top: 11px;
  }
  .sendCode {
    border-left: 1px solid #c5c6cd;
    position: absolute;
    top: 0;
    right: 0;
    padding: 0 14px;
    font-size: 14px;
    height: 50px;
    line-height: 50px;
    font-weight: 400;
    color: #165dff;
    cursor: pointer;
    &:hover {
      font-weight: 500;
    }
  }
</style>
